

$(function(){
	
	Vue.component({
		
	});
	
	//1、全局token开始
	var url =  window.parent.parent.location.href;
	var token = url.split("token")[1];
	token=token.slice(1);
	console.log("role-manager.html的token="+token);
	//1、全局token结束
	
	//2、解析出上下文逻辑开始
	var context = getRootPath();
	console.log("context="+context);
	//2、解析出上下文逻辑结束
	
	
	//全局变量
	var pageNumber=1;
	var pageSize=5;
	var name='';
	function getUrl(pageNumber,pageSize){
		//var pageNumber=vue.pageNumber;
		//var pageSize=vue.pageSize;
		var url=context+'clazz/findAll?pageNumber='+pageNumber+'&pageSize='+pageSize;
		console.log("请求分页数据url="+url);
		return url;
	}
	
	
	//3、角色分页列表页面开始
	var vue =new Vue({
		el:'#main',
		data:{
			clazzStudents:[],
			clazzs:[],
			ids:[],
			html:'',
			className:'',
			// pageNumber 第几页
			pageNumber:pageNumber,
			
			// pageSize   1页多少条
			pageSize:pageSize,
			
			// totalPage 共多少页
			totalPage:0,
			
			// totalCounter 一共多少条记录
			totalCounter:0
		},
		methods:{
			
			// 搜索框的功能
			findByclassName:function(pageNumber,pageSize){
				var url=context+"userRole/findAllLimitByclassName";
				var data={
					pageNumber:vue.pageNumber,
					pageSize:vue.pageSize,
					className:vue.className
				}
				axios.post(url,data).then(function(dataR){
					console.log(dataR);
					var code =dataR.data.code;
					
					var data=dataR.data.data;
					var clazzStudents=data.ClazzStudentList;
					
					var pageNumber=data.pageNumber;
					var totalCounter=data.totalCounter;
					var totalPage=data.totalPage;
					
					vue.clazzStudents=clazzStudents;
					
					if(vue.className!=name){
						vue.pageNumber=1;
					}else{
						vue.pageNumber=pageNumber;
					}
					vue.totalCounter=totalCounter;
					vue.totalPage=totalPage;
					vue.clazzs=data.roleList;
					
					
				});
			},
			
			
			//拿到角色分页列表的数据
			getRoleListPage:function(pageNumber,pageSize){
				axios.get(getUrl(pageNumber,pageSize)).then(function(dataR){
					console.log(dataR);
					var code =dataR.data.code;
					
					var data=dataR.data.data;
					var clazzStudents=data.clazzStudents;
					
					var pageNumber=data.pageNumber;
					var totalCounter=data.totalCounter;
					var totalPage=data.totalPage;
					
					vue.clazzStudents=clazzStudents;
					vue.pageNumber=pageNumber;
					vue.totalCounter=totalCounter;
					vue.totalPage=totalPage;
					vue.clazzs=data.clazzs;
					
				});
			},
			
			tableAddButton:function(index){
				console.log("index="+index);
				console.log(vue.clazzs);
				var clazzStudent = this.clazzStudents[index];
				
				var studentId =clazzStudent.studentId;
				var studentName=clazzStudent.studentName;
				//1.1：填充表格
				$('p[name="userId"]').text(studentId);
				$('p[name="userName"]').text(studentName);
				var html = '';
				      for (var i = 0; i < vue.clazzs.length; i ++) {
				        html += "<option id="+vue.clazzs[i].id+" name='roleId'>" + vue.clazzs[i].name + "</option>";
				      }
				$('#classNameList').append(html);
				
				//1.2：打开模态框
				$('#addClazzStudentTeacherBean').modal({
					backdrop:"static",
				});
			}
		}
	});
	name=vue.className;
	vue.getRoleListPage(pageNumber,pageSize);
	//3、角色分页列表页面结束
	
	
	//单击绑定角色模块框中的保存按钮 开始-------------------------------------- 
	$('#modalUpdateRoleButton').click(function(){
		
		//2.1:收集表单数据并向后台发送请求
		var userId=$("p[name='userId']").text();
		var roleId=$("#classNameList").find('option:selected').attr('id');
		var data={
			userId:userId,
			clazzId:roleId,
			token:token
		}
		
		var url=getRootPath()+'assign/clazz/student';
		var callback=function(dataR){
			var code=dataR.code;
			if(code==200){
				//2.2:关闭修改模态框
				alert("-----给学生分配班级成功------");
				$('#addClazzStudentTeacherBean').modal('hide');
				$("option").remove();
				//2.3:刷新刷新页面数据
				vue.getRoleListPage(1,pageSize);
			}else{
				alert('学生已分配班级，分配失败');
			}
		};
		send(url,data,callback);
	});
	//单击绑定角色模块框中的保存按钮 结束-------------------------------------- 
	$('#guanbi').click(function(){
		$("option").remove();
	})
	
	//全选功能开始
	$('#selectAllRole').click(function(){
		$('.check-role').prop('checked',$(this).prop('checked'));
	});
	
	//表格中的复选框
	$(document).on('click','.check-role',function(){
		var flag= $('.check-role:checked').length==$('.check-role').length;
		$('#selectAllRole').prop('checked',flag);
	});
	
	//批量删除的逻辑开始-------------------
	$('#deleteButton').click(function(){
		
		//把原来的选中状态清除
		$('.check-role').prop('checked',false);
		var ids=[];
		
		//3 往后台发送数据
		var indexs=vue.ids;
		for (var i = 0; i < indexs.length; i ++) {
			var index=indexs[i];
			var userRole=vue.clazzStudents[index];
			ids.push(userRole.id);
		}
		var data={
			"ids":ids,
		}
		var url=getRootPath()+"delete/clazz/student";
		var callback=function(dataR){
			var code=dataR.code;
			if(code==200){
				//刷新刷新页面数据
				alert('删除成功')
				vue.getRoleListPage(1,pageSize);
				
			}
			
		};
		send(url,data,callback);
		
	});
	//批量删除的逻辑结束-------------------
	
	
	//3、下1页
	$('#nextPage').click(function(){
		if(vue.pageNumber<vue.totalPage){
			vue.pageNumber=vue.pageNumber+1;
			if(vue.className!=''){
				vue.findByclassName(vue.pageNumber,vue.pageSize);
			}else{
				vue.getRoleListPage(vue.pageNumber,vue.pageSize);
			}
		}
	});
	//3、下1页
	
	//3、最后1页
	$('#lastPage').click(function(){
		if(vue.pageNumber<vue.totalPage){
			if(vue.className!=''){
				vue.findByclassName(vue.pageNumber,vue.pageSize);
			}else{
				vue.getRoleListPage(vue.pageNumber,vue.pageSize);
			}
		}
	});
	//3、最后
	
	//4、上1页
	$('#prePage').click(function(){
		if(vue.pageNumber>=2){
			vue.pageNumber=vue.pageNumber-1;
			if(vue.className!=''){
				vue.findByclassName(vue.pageNumber,vue.pageSize);
			}else{
				vue.getRoleListPage(vue.pageNumber,vue.pageSize);
			}
		}
	});
	
	//5、第1页
	$('#firstPage').click(function(){
		if(vue.className!=''){
			vue.findByclassName(vue.pageNumber,vue.pageSize);
		}else{
			vue.getRoleListPage(vue.pageNumber,vue.pageSize);
		}
	});
	
});
